<?php
class hschool_trade{
	public $info_array = array();
	public $page_array = array();
	public $tree_array = array();
	public static $all_course_array = array();

	static function &instance() {
		static $object;
		if(empty($object)) {
			$object = new self();
		}
		return $object;
	}

	/*
	*	取出一个信息
	*/
	public function get_one($id='') {
		$info = array();
		if($id){
			$sql = "SELECT * FROM ".DB::table('trade')." WHERE trade_id='".$id."' LIMIT 1";
			$info = DB::fetch_first($sql);	
		}
		return $info;
	}

	/*
	*	取出一个信息 通过 用户编号 user_id 产品类别 trade_type 产品编号 trade_opt_id 
	*/
	public function get_one_param($user_id, $trade_opt_id, $trade_type=1) {
		$info = array();
		if($user_id && $trade_opt_id){
			$sql = "SELECT * FROM ".DB::table('trade')." WHERE user_id='".$user_id."' AND trade_type='".$trade_type."' AND trade_opt_id='".$trade_opt_id."' LIMIT 1";
			$info = DB::fetch_first($sql);	
		}
		return $info;
	}

	/*
	*	创建
	*/
	public function add() {
		global $_G;
		$is_submit = isset($_REQUEST['is_submit']) ? $_REQUEST['is_submit']:'';
		if(!empty($is_submit)){
			$id = isset($_REQUEST['id']) ? $_REQUEST['id']:'';
		}else{

		}
	}

	/*
	*	编辑
	*/
	public function edit() {
		global $_G;
		$is_submit = isset($_REQUEST['is_submit']) ? $_REQUEST['is_submit']:'';
		if(!empty($is_submit)){

		}else{
			$id = isset($_REQUEST['id']) ? $_REQUEST['id']:'';
		}
	}

	/*
	*	删除
	*/
	public function del() {
		$is_submit = isset($_REQUEST['is_submit']) ? $_REQUEST['is_submit']:'';
		if(!empty($is_submit)){
			$course_ids_array = array();
			$course_ids_array = isset($_REQUEST['trade_ids']) ? $_REQUEST['trade_ids']:array();
			if($course_ids_array){
				$update = array(
					'isdelete'=> 1
				);
				$where = ' id IN ('.dimplode($course_ids_array).') ';
				$effects = DB::update('trade',$update,$where);
			}
			//DEBUG 接收用户信息 end
			$this->return_json(false,'操作成功');
			echo json_encode($return_array);
		}
	}

	/*
	*	当前动作的首页
	*/
	public function index() {
		global $_G;
		$page_array = array();
		$sqlcount = $sql = $wheresql = $title_keyword = $pageNum = $numPerPage = '';
		$pageNum = isset($_REQUEST['pageNum']) ? $_REQUEST['pageNum'] : 1;
		$numPerPage = isset($_REQUEST['numPerPage']) ? $_REQUEST['numPerPage'] : 10;
		$pagestart = ($pageNum - 1) * $numPerPage;
		$hschool_dept = load_class('hschool','dept');
		if($hschool_dept){
			$d_id_array=array();
			$d_id_array = $hschool_dept->get_sub_tree($_G['dept_id']);
			if(!empty($d_id_array)){
				$d_id_array=array_keys($hschool_dept->tree2array($d_id_array));
			}
			if($_G['member']['is_dept_master']){
				$d_id_array[] = $_G['dept_id'];
			}
		}
		//$wheresql .= " AND ( c.dept_id IN (".dimplode($d_id_array).") OR create_id ='".$_G['user_id']."' ) ";
		//if($course_type){
		//	$wheresql .= " AND c.type = '".$course_type."'";
		//}
		$sqlcount = "SELECT count(*) from ".DB::table('trade')." AS t WHERE c.isdelete=0 ".$wheresql;
		$pagetotal = DB::result_first($sqlcount);
		$sql = "SELECT t.* from ".DB::table('trade')." AS t WHERE t.isdelete=0 ".$wheresql." ORDER BY t.id DESC LIMIT ".$pagestart.",".$numPerPage;
		$page_array = DB::fetch_all($sql);
		foreach($page_array AS $key => $value){

		}
		//总页数
		$realpages = @ceil($pagetotal / $numPerPage);
		if($realpages <= 0){
			$realpages=1;
		}
		//前一页
		$page_pre = max(1, intval(($pageNum-1)));
		//后一页
		$page_next = min(intval(($pageNum+1)), $realpages);
		include template('admin/hschool/trade');
	}

	/*
	* 获取交易状态名称
	*/
	public function get_trade_status_name($trade_status) {
		$trade_status_name = '';
		switch($trade_status){
			case 'TRADE_PENDING':
				$trade_status_name = "待收款";
				break;
			case 'WAIT_BUYER_PAY':
				$trade_status_name = "待付款";
				break;
			case 'TRADE_SUCCESS':
				$trade_status_name = "已付款";
				break;
			case 'REFUND_SUCCESS':
				$trade_status_name = "订单部分退款";
				break;
			case 'REFUND_CLOSED':
				$trade_status_name = "订单全额退款";
				break;
			case 'TRADE_FINISHED':
				$trade_status_name = "交易完成";
				break;
			case 'TRADE_CLOSED':
				$trade_status_name = "交易取消";
				break;
			case 'TRADE_PART_SUCCESS':
				$trade_status_name = "订单部分付款";
				break;
			case 'TRADE_FREE':
				$trade_status_name = "免费学员";
				break;
		}
		return $trade_status_name;
	}

	/*
	* 获取交易方式名称
	*/
	public function get_trade_way_name($trade_way) {
		$trade_way_name = '';
		switch($trade_way){
			case '1':
				$trade_way_name = "支付宝";
				break;
			case '2':
				$trade_way_name = "现场缴费";
				break;
		}
		return $trade_way_name;
	}

	/*
	* 获取取消交易与选课绑定
	* 仅未支付状态的交易可取消
	*/
	public function trade_cancel($trade_id) {
		global $_G;
		$error = 'true';
		$message = '操作失败';
		if($trade_id && $_G['user_id']){
			$sql = "SELECT * FROM ".DB::table('trade')." WHERE trade_id='".$trade_id."' AND user_id='".$_G['user_id']."' LIMIT 1";
			$trade_result = DB::fetch_first($sql);
			if('WAIT_BUYER_PAY'==$trade_result['trade_status']){
				//取消选课与订单绑定
				$where = array('uid'=>$_G['user_id'],'trade_id'=>$trade_id);
				$effects1 = @DB::delete('student_course', $where);
				//更新订单状态
				$data = array('trade_status'=>'TRADE_CLOSED');
				$where = array('user_id'=>$_G['user_id'],'trade_id'=>$trade_id);
				$effects2 = @DB::update('trade', $data, $where);
				//插入订单变更日志记录
				$trade_result['trade_status'] = 'TRADE_CLOSED';
				$effects3 = @DB::insert('trade_log', $trade_result,true);
				if($effects1 && $effects2 && $effects3){
					$error = 'false';
					$message = '操作成功';
				}
			}
		}
		return self::return_json($error,$message);
	}

	/*
	* ajax 返回信息
	*/
	public function return_json($error='true',$message="操作失败") {
		$return_array = array(
			"error" => $error,
			"message" => $message
		);
		echo json_ext($return_array);
		die;
	}
}
?>